#ifndef _DSWALARMCENTER_H
#define _DSWALARMCENTER_H

#include "base.h"

#define MAX_ALARM_FUNC 64
#define NEW_ALARM -1

class CDswalarmcenter
{
public:
	CDswalarmcenter(LPST_ENV_T pEnv);
	virtual ~CDswalarmcenter();
private:
	static void cbTimerTask(int fd, short events, void* ctx){((CDswalarmcenter*)ctx)->OnTimer(fd, events);};
	void OnTimer(int fd, short events);
	LPST_ALARM_MSG_T ProcessAlarmMsg(LPST_ALARM_PARAM pAlarmParam, LPST_ALARM_FUNC_T pFuncNode, LPST_ALARM_MSG_T pNode, bool b1S, bool &bNeedToProcess);
	static void* cbAlarmProcessThread(void* pParam){((CDswalarmcenter*)pParam)->AlarmProcessThread();return NULL;};
	void AlarmProcessThread();
	LPST_ALARM_FUNC_T SeekAlarmFunc(unsigned int nAreID, LPST_ALARM_FUNC_T pSourceFunc, int nCount);
public:
	void LoadAlarmCfg();
public:
	static void cbOnProcessAction(int nAreId, void* pAlarmParam, void* pCaller){((CDswalarmcenter*)pCaller)->OnProcessAction(nAreId, pAlarmParam);};
	static void cbOnAcDc(int nAreId, void* pAlarmParam, int nEnalarm, void* pCaller){((CDswalarmcenter*)pCaller)->OnAcDc(nAreId, pAlarmParam, nEnalarm);};
	static void cbOnOffLine(int nAreId, void* pAlarmParam, int nEnalarm, void* pCaller){((CDswalarmcenter*)pCaller)->OnOffLine(nAreId, pAlarmParam, nEnalarm);};
	static void cbOnLPower(int nAreId, void* pAlarmParam, int nEnalarm, void* pCaller){((CDswalarmcenter*)pCaller)->OnLPower(nAreId, pAlarmParam, nEnalarm);};
	static void cbOnSOS(int nAreId, void* pAlarmParam, int nEnalarm, void* pCaller){((CDswalarmcenter*)pCaller)->OnSOS(nAreId, pAlarmParam, nEnalarm);};

private:
	void CheckPower();
	void CheckOnOffLine();
	void CheckDiskSpace();

	void OnProcessAction(int nAreId, void* pAlarmParam);
	
	void OnAcDc(int nAreId, void* pAlarmParam, int nEnalarm);
	void OnOffLine(int nAreId, void* pAlarmParam, int nEnalarm);
	void OnLPower(int nAreId, void* pAlarmParam, int nEnalarm);
	void OnSOS(int nAreId, void* pAlarmParam, int nEnalarm);
private:
	LPST_ENV_T m_pEnv;
	timer_param_t m_nTimer;

	pthread_mutex_t m_alarmcfg_lock;
	ST_ALARM_PARAM m_alarmcfg;
	ST_ALARM_FUNC_T m_alarm_func[MAX_ALARM_FUNC];

	ST_LIST_ALARM_MSG_T m_alarm_msg, m_process_msg;
	int m_n100ms, m_n60S;

	pthread_t m_thread_id;

	EN_POWER_TYPE m_nOldPwrStatus;
	bool m_bOldLinkStatus, m_bOldLowPower;

	time_t m_nLockTime;
};

#endif

